package com.scau.os.extension;

import java.util.ArrayList;
import java.util.List;

public class EasyDelegate extends MyDelegate{

    public EasyDelegate(String n) {
        super(n);
    }
    private List<MyDelegate>toDoList=new ArrayList<>();
    @Override
    public void execute(Object data) {
        for (MyDelegate m:
                toDoList) {
           m.execute(data);
        }
    }

    /**
     * 返回事件的数量
     * @return
     */
    public Integer size(){
        return toDoList.size();
    }
    public IUnRegister add(MyDelegate myDelegate){
        toDoList.add(myDelegate);
        return new CostomUnRegister(()->{
           delete(myDelegate);
        });
    }

    public void delete(MyDelegate myDelegate){
        for (MyDelegate m:
             toDoList) {
            if(m.cmp(myDelegate)){
                toDoList.remove(m);
                break;
            }
        }
    }
    public void clear(){
        toDoList.clear();
    }
}
